package com.yfp.machine.service.impl;

import com.kangaroohy.dingtalk.service.IDingTalkSender;
import com.yfp.common.core.constant.DbConstants;
import com.yfp.common.core.utils.DateUtils;
import com.yfp.common.core.utils.StringUtils;
import com.yfp.common.core.utils.WeixinRobotSender;
import com.yfp.emqx.MqttAcceptClient;
import com.yfp.machine.domain.Machine;
import com.yfp.machine.service.MachineService;
import com.yfp.machine.service.MqService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Slf4j
public class MqServiceImpl implements MqService {

    @Resource
    MqttAcceptClient mqttAcceptClient;

    @Autowired
    private IDingTalkSender talkSender;

    @Resource
    MachineService machineService;

    @Value("${mqtt.topic.dev}")
    private String topic;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void subscriptionMessage() {
        log.info("MqService订阅消息主题 ===>:  " + topic);
        mqttAcceptClient.subscribe(topic, 2, (top, message) -> {
            try {
                log.info("MqService接收消息主题 ===>:  " + top);
                log.info("MqService接收消息Qos ===>: " + message.getQos());
                log.info("MqService接收消息内容 ===>: " + new String(message.getPayload()));

                Pattern pattern = Pattern.compile("\\d+");  // 匹配数字部分的正则表达式
                Matcher matcher = pattern.matcher(top);
                if (matcher.find()) {
                    String machineSn = matcher.group();
                    if (Objects.isNull(machineSn)){
                        return;
                    }
                    Machine machine = new Machine();
                    machine.setMachineId(machineSn);
                    machine.setOnlineStatus(DbConstants.machineCorrelation.MACHINE_STATUS_OFFLINE);
                    machine.setOperationStatus(2);
                    machine.setOfflineUpdateTime(new Date());
                    Machine update = machineService.update(machine);
                    log.info("MqService更新设备状态成功 ===>: " + machineSn);
                    if (StringUtils.isNotEmpty(update.getMachineSn())) {
                        String text = "设备离线通知" +
                                "\n - 设备编号： " + update.getMachineSn() +
                                "\n - 设备名称： " + update.getMachineName() +
                                "\n - 离线时间： " + DateUtils.getTime();
                        WeixinRobotSender.sendTextMessage(text, applicationContext);
                    }
//                talkSender.send(MarkdownArgs.builder().title("设备离线通知").text(text).build());
                }
            } catch (Exception e) {
                log.error("subscriptionMessage" + e);
            }
        });
    }

}
